// pages/mine/mine.js
const app = getApp();
var ages = new Array(100);
for (var i = 1; i <= 100; i++) {
  ages[i - 1] = i;
}
var xtarr = [];
var i = 3.8;
while (i < 10) {
  i = i + 0.1;
  xtarr.push(i.toFixed(1));
}
var dgcarr = [];
var i = 2.0;
while (i < 7) {
  i = i + 0.1;
  dgcarr.push(i.toFixed(1));
}
var gyszarr = [];
var i = 0.0;
while (i < 2) {
  i = i + 0.1;
  gyszarr.push(i.toFixed(1));
}
var nsarr = [];
var i = 150;
while (i < 500) {
  i = i + 10;
  nsarr.push(i);
}

var steparr = [];
var i = 4000;
while (i < 30000) {
  i = i + 1000;
  steparr.push(i);
}
var nowDate = new Date();
var year = nowDate.getFullYear();
var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
  : nowDate.getMonth() + 1;
var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
  .getDate();
var dateStr = year + "-" + month + "-" + day;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    url: app.globalData.url,
    index: 0,
    genders: ['男','女'],
    ages: ages,
    xts: xtarr,
    nss: nsarr,
    dgcs: dgcarr,
    gyszs: gyszarr,
    steps: steparr,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    //可以通过hidden是否掩藏弹出框的属性，来指定那个弹出框  
    hiddenAge: true,
    hiddenStep: true,
    hiddenXt: true,
    hiddenNs: true,
    hiddenGysz: true,
    hiddenDgc: true,
    gender:2,
    age: 0,
    step: 0,
    cqxt: 0,
    chxt: 0,
    ns: 0,
    gysz: 0,
    dgc: 0,
    end: dateStr,
    auth: false
  },
  getNow:function(){
   
    return dateStr;
  },
  bindGenderChange: function (e) {
    this.setData({
      gender: (parseInt(e.detail.value)+1)
    });
    this.save('gender', this.data.gender);
  },
  bindAgeChange: function (e) {
    this.setData({
      age: e.detail.value
    });
    this.save('age', this.data.age);
  },
  bindCqxtChange: function (e) {
    this.setData({
      cqxt: xtarr[e.detail.value]
    });
    this.save('xt', this.data.cqxt, this.data.chxt);
  },
  bindChxtChange: function (e) {
    this.setData({
      chxt: xtarr[e.detail.value]
    });
    this.save('xt', this.data.cqxt, this.data.chxt);
  },
  bindNsChange: function (e) {
    this.setData({
      ns: this.data.nss[e.detail.value]
    });
    this.save('ns', this.data.ns);
  },
  bindGyszChange: function (e) {
    this.setData({
      gysz: this.data.gyszs[e.detail.value]
    });
    this.save('gysz', this.data.gysz);
  },
  bindDgcChange: function (e) {
    this.setData({
      dgc: this.data.dgcs[e.detail.value]
    });
    this.save('dgc', this.data.dgc);
  },
  bindStepChange: function (e) {
    this.setData({
      step: this.data.steps[e.detail.value]
    });
    this.save('step', this.data.step);
  },

  onShow:function(){
    app.get3Session();
    this.myShow();
  },

  myShow:function(){
    var self = this;
    var start = new Date();

    self.setData({
      auth: true
    });
    if (!this.data.auth) {
      return;
    }
    wx.setNavigationBarTitle({
      title: '我的'
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        gender: app.globalData.userInfo.gender,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          gender: res.userInfo.gender,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    self.loadTarget();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  loadTarget: function () {
    var threeSession = app.get3Session();
    if (threeSession == null || threeSession == '' || threeSession == undefined) {
      app.loadTarget = res => {
        var threeSession = app.get3Session();
        var self = this;
        wx.request({
          url: app.globalData.domain + "/user/mine",
          data: {
            threeSession: threeSession
          },
          success: function (mine) {
            if (mine.data == null || mine.data == '' || mine.data == 'fail') {
              return
            }
            if (mine.data.user != null) {
              self.setData({
                age: mine.data.user.age
              })
            }
            if (mine.data.target != null) {
              self.setData({
                step: mine.data.target.targetStep,
                cqxt: mine.data.target.targetXtCq,
                chxt: mine.data.target.targetXtCh,
                ns: mine.data.target.targetNs,
                gysz: mine.data.target.targetGysz,
                dgc: mine.data.target.targetDgc
              });
            }
          }
        });
      }
    } else {
      var self = this;
      wx.request({
        url: app.globalData.domain + "/user/mine",
        data: {
          threeSession: threeSession
        },
        success: function (mine) {
          if (mine.data == null || mine.data == '' || mine.data == 'fail') {
            return
          }
          if (mine.data.user != null) {
            self.setData({
              age: mine.data.user.age
            })
          }
          if (mine.data.target != null) {
            self.setData({
              step: mine.data.target.targetStep,
              cqxt: mine.data.target.targetXtCq,
              chxt: mine.data.target.targetXtCh,
              ns: mine.data.target.targetNs,
              gysz: mine.data.target.targetGysz,
              dgc: mine.data.target.targetDgc
            });
          }
        }
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
    }
    return {
      title: 'MEDATA',
      path: 'pages/step/step',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //确认  
  confirm: function (e) {
    var datatype = e.currentTarget.dataset.type;
    if (datatype == 'age') {
      this.save(datatype, this.data.age);
      this.setData({
        hiddenAge: true
      });
    } else if (datatype == 'step') {
      this.save(datatype, this.data.step, null)
      this.setData({
        hiddenStep: true
      });
    } else if (datatype == 'xt') {
      this.save(datatype, this.data.cqxt, this.data.chxt)
      this.setData({
        hiddenXt: true
      });
    } else if (datatype == 'ns') {
      this.save(datatype, this.data.ns)
      this.setData({
        hiddenNs: true
      });
    } else if (datatype == 'gysz') {
      this.save(datatype, this.data.gysz)
      this.setData({
        hiddenGysz: true
      });
    } else if (datatype == 'dgc') {
      this.save(datatype, this.data.dgc)
      this.setData({
        hiddenDgc: true
      });
    }

  },

  save: function (types, value, value1) {
    const threeSession = app.get3Session();
    var self = this;
    wx.request({
      url: app.globalData.domain + 'user/setTarget?threeSession=' + threeSession,
      data: {
        threeSession: threeSession,
        types: types,
        value: value,
        value1: value1
      },
      dataType: 'json',
      success: function (data) {
        app.getTarget(threeSession);
      }
    })
  }
})